package com.zmn.oms.api.listener;

import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.manager.utils.SpringBeanFactoryUtil;
import com.zmn.oms.model.dto.order.create.CreateOrderFailMessage;
import com.zmn.oms.model.entity.warn.OrderWarnReplyEvent;
import com.zmn.oms.model.entity.work.OrderWorkLogChange;
import com.zmn.oms.third.daowei.DaoweiConsts;
import com.zmn.oms.third.deshiman.DeshimanConsts;
import com.zmn.oms.third.dj58.DaoJia58Consts;
import com.zmn.oms.third.hb114.Haobai114Consts;
import com.zmn.oms.third.jingdong.JingdongConsts;
import com.zmn.oms.third.koubei.KoubeiChannelEnum;
import com.zmn.oms.third.pinduoduo.util.PinDuoDuoUtil;
import com.zmn.oms.third.taobao.TaobaoMapper;
import com.zmn.oms.third.tc58.Tc58Consts;
import com.zmn.oms.third.youzan.YouzanConsts;
import com.zmn.oms.third.zhimi.ZhimiConsts;
import com.zmn.oms.zmn.business.interfaces.open.OrderChangeNotifyService;
import com.zmn.oms.zmn.business.interfaces.open.OrderNewNotifyService;
import com.zmn.oms.zmn.business.interfaces.open.OrderWarnReplyNotifyService;
import com.zmn.oms.zmn.facade.interfaces.open.EntrustOrderNotifyFacade;
import com.zmn.oms.zmn.facade.interfaces.order.CreateOrderFacade;

import java.util.Optional;

/**
 * 类描述：状态推送工厂类
 *
 * @author liuying
 * @date 2019/06/13 20:38
 */
public class OrderChangeNotifyFactory {

    public static OrderChangeNotifyService getInstance(OrderWorkLogChange orderWorkLogChange) {
        int channelId = Optional.ofNullable(orderWorkLogChange.getChannelId()).orElse(-1);

        switch (channelId) {
            case JingdongConsts.CHANNEL_ID: // 京东服务+
                return SpringBeanFactoryUtil.getBean("jingdongOrderChangeNotifyService");

            case Tc58Consts.CHANNEL_ID_VIP: // 58同城VIP（58同城114管家）
            case Tc58Consts.CHANNEL_ID_FWK: // 58同城服务卡
                return SpringBeanFactoryUtil.getBean("tc58NotifyService");

            case DaoJia58Consts.CHANNEL_ID_API: // 58到家直约
            case DaoJia58Consts.CHANNEL_ID_APARTMENT: // 58到家-同城公寓
                return SpringBeanFactoryUtil.getBean("daoJia58NotifyService");

            case DaoJia58Consts.CHANNEL_ID_DJ58_REWORK: // 58到家维保返修
                return SpringBeanFactoryUtil.getBean("daoJiaReworkNotifyService");

            case OrderConsts.CHANNEL_ID_GUOMEI_MYBQX: // 国美美云保清洗
                return SpringBeanFactoryUtil.getBean("gmqxNotifyService");

            // V2版接口上线前已接入渠道
            case OrderConsts.CHANNEL_ID_GUANGZHOU_HAOBAI_WECHAT: // 广州号百公众号
            case OrderConsts.CHANNEL_ID_KEBIYOUJU: // 雄天集团可比优居
            case OrderConsts.CHANNEL_ID_TRANSPOPO: // 铁通会员
                return SpringBeanFactoryUtil.getBean("defaultNotifyService");

            // 到位
            case DaoweiConsts.CHANNEL_ID_KYX: // 到位快易修
            case DaoweiConsts.CHANNEL_ID_APP: // 到位APP
            case DaoweiConsts.CHANNEL_ID_DTW: // 到位电通王
            case DaoweiConsts.CHANNEL_ID_ZX: // 到位直选
                return SpringBeanFactoryUtil.getBean("daoweiNotifyService");

            case DeshimanConsts.CHANNEL_ID: // 德施曼
                return SpringBeanFactoryUtil.getBean("deshimanNotifyService");

            case ZhimiConsts.CHANNEL_ID: // 智米
                return SpringBeanFactoryUtil.getBean("zhimiOrderNotifyService");

            case YouzanConsts.CHANNEL_ID: // 有赞
                return null; // 有赞不需要同步订单状态

            case OrderConsts.CHANNEL_ID_JD_HUADI: // 京东华帝
                return SpringBeanFactoryUtil.getBean("jdHuadiNotifyService");

            case OrderConsts.CHANNEL_ID_BAIDU_APP: // 百度小程序
                return SpringBeanFactoryUtil.getBean("baiduAppNotifyService");

            case OrderConsts.CHANNEL_ID_YUNDING_LOCK: // 云丁鹿客
                return SpringBeanFactoryUtil.getBean("yundingNotifyService");

            case OrderConsts.CHANNEL_ID_SF_INSTALL: // 顺丰安装
                return SpringBeanFactoryUtil.getBean("shunfengNotifyService");

            case OrderConsts.CHANNEL_ID_SUNING_BW: // 苏宁保外
                return SpringBeanFactoryUtil.getBean("suningNotifyService");

            case OrderConsts.CHANNEL_ID_JD_DAOJIA: // 京东到家
                return SpringBeanFactoryUtil.getBean("jddjNotifyService");

            case OrderConsts.CHANNEL_ID_GOME_GJ_JDWX:  // 国美管家
            case OrderConsts.CHANNEL_ID_GOME_GJ_ZX:
                return SpringBeanFactoryUtil.getBean("gmgjNotifyService");

            case OrderConsts.CHANNEL_ID_TMALL_QINGE_BW:
                return SpringBeanFactoryUtil.getBean("tmallNotifyService");

            default:
                // 拼多多
                if (PinDuoDuoUtil.isPinDuoDuoChannel(channelId)) {
                    return SpringBeanFactoryUtil.getBean("pinDuoDuoNotifyService");
                }
                // 114号百平台
                else if (Haobai114Consts.isHaobai114SyncChannel(channelId)) {
                    return SpringBeanFactoryUtil.getBean("haobai114NotifyService");
                }
                // 天猫订单
                else if (TaobaoMapper.isTmallChannel(channelId)) {
                    return SpringBeanFactoryUtil.getBean("tmallOrderChangeNotifyService");
                }
                // 口碑订单
                else if (KoubeiChannelEnum.isKoubeiChannel(channelId)) {
                    return SpringBeanFactoryUtil.getBean("koubeiNotifyService");
                }
                // 其他
                else {
                    return SpringBeanFactoryUtil.getBean("v2OrderChangeNotifyService");
                }
        }
    }

    /**
     * 新单同步
     * @param orderWorkLogChange
     * @return
     */
    static OrderNewNotifyService getNewOrderNotifyInstance(OrderWorkLogChange orderWorkLogChange) {
        int channelId = Optional.ofNullable(orderWorkLogChange.getChannelId()).orElse(-1);

        switch (channelId) {

            // V2版接口上线前已接入渠道
            case OrderConsts.CHANNEL_ID_GUANGZHOU_HAOBAI_WECHAT: // 广州号百公众号
            case OrderConsts.CHANNEL_ID_KEBIYOUJU: // 雄天集团可比优居
            case OrderConsts.CHANNEL_ID_TRANSPOPO: // 铁通会员
                return SpringBeanFactoryUtil.getBean("defaultNotifyService");

            case Tc58Consts.CHANNEL_ID_VIP: // 58同城VIP（58同城114管家）
            case Tc58Consts.CHANNEL_ID_FWK: // 58同城服务卡
                return SpringBeanFactoryUtil.getBean("tc58NotifyService");

            case Tc58Consts.CHANNEL_ID_JX: // 58同城-到家精选
            case Tc58Consts.CHANNEL_ID_JX_CLOCK: // 58同城-到家精选-开锁
            case Tc58Consts.CHANNEL_ID_JX_PIPE: // 58同城-到家精选-管道疏通
                return SpringBeanFactoryUtil.getBean("tc58NotifyService");

            case OrderConsts.CHANNEL_ID_DJ58_DAG: // 58到家直约
                return SpringBeanFactoryUtil.getBean("daoJia58NotifyService");

            case DaoJia58Consts.CHANNEL_ID_APARTMENT: // 58到家-同城公寓
                return SpringBeanFactoryUtil.getBean("daoJia58NotifyService");

            case OrderConsts.CHANNEL_ID_GUOMEI_MYBQX: // 国美美云保清洗
                return SpringBeanFactoryUtil.getBean("gmqxNotifyService");

            case ZhimiConsts.CHANNEL_ID: // 智米，给对方同步接单应答消息
                return SpringBeanFactoryUtil.getBean("zhimiOrderNotifyService");

            case OrderConsts.CHANNEL_ID_JD_HUADI: // 京东华帝，给对方同步接单应答消息
                return SpringBeanFactoryUtil.getBean("jdHuadiNotifyService");

            default:
                // 拼多多
                if (PinDuoDuoUtil.isPinDuoDuoChannel(channelId)) {
                    return SpringBeanFactoryUtil.getBean("pinDuoDuoNotifyService");
                }
                // 百度小程序
                else if (OrderConsts.CHANNEL_ID_BAIDU_APP == channelId) {
                    return SpringBeanFactoryUtil.getBean("baiduAppNotifyService");
                }
                // 其他
                else {
                    return SpringBeanFactoryUtil.getBean("v2OrderChangeNotifyService");
                }
        }
    }

    /**
     * 订单预警回复
     * @param orderWarnReplyEvent
     * @return
     */
    static OrderWarnReplyNotifyService getOrderWarnReplyNotifyInstance(OrderWarnReplyEvent orderWarnReplyEvent) {
        Integer channelId = orderWarnReplyEvent.getChannelId();
        if (TaobaoMapper.isTmallChannel(channelId)) {
            return SpringBeanFactoryUtil.getBean("tmallOrderWarnReplyNotifyService");
        }
        else {
            return null;
        }
    }

    /**
     * 委托单新单推送类
     * @param orderWorkLogChange
     * @return
     */
    static EntrustOrderNotifyFacade getNewEntrustOrderNotifyInstance(OrderWorkLogChange orderWorkLogChange) {
        return SpringBeanFactoryUtil.getBean("entrustOrderNotifyFacade");
    }

    /**
     * 新单创建失败推送类
     * @param createOrderFailMessage
     * @return
     */
    static CreateOrderFacade getOrderCreateFailNotifyInstance(CreateOrderFailMessage createOrderFailMessage) {
        return SpringBeanFactoryUtil.getBean("createOrderFacade");
    }


}
